#!/usr/bin/python
# -*- coding:utf-8 -*-

import os
import sys
import jieba
sys.path.append(os.path.join(os.path.split(os.path.realpath(__file__))[0], "libs"))
import elasticsearchutil as esutil

fpath = "a.txt"

es_obj = esutil.EsConnect("10.20.32.138:9200", "test-notes-2017.5", "message")
esquery_body = {
    "filter":{
        "range":{
            "time": {
                "gte": 1493568000,              #20170501
                "lte": 1493654400,              #20170502
                "format": "epoch_second"
            }
        }
    }
}
es_res = es_obj.es_query(esquery_body, 10000)

with open(fpath, "w") as f:
    for item in es_res:
        seg_list = jieba.cut(item['_source']['text'], cut_all=False)

        line = " ".join(seg_list)

        f.writelines(line + '\n')
    f.close()
